function [First_Sample] = HF_Time_Synch(RX_Faded_Noisy,Original_Signal,...
    Oversample,Paket_Start,HF_SRRC,with_carr)



% RX_Faded_Noisy = RX_Faded_Noisy*exp(-1j*1.6641);

% Convlve with Folter SRRC Filter ...
% RX_Faded_Noisy = conv(RX_Faded_Noisy,HF_SRRC);
% Original_Signal = conv(Original_Signal,HF_SRRC);

global PFO_setimation
global Sync_Preamble Mini_Probe_Plus

HF_SSRRC_Len = floor(length(HF_SRRC)/2)+24*Oversample/4;

% Mini_Probe_Plus_Modulated =  HF_Modulate(Mini_Probe_Plus,4800);
% Initial_Sync_Preamble = HF_Modulate(Sync_Preamble,4800);

% Mini_Probe_Plus_Modulated_Upsampled = upsample(Mini_Probe_Plus_Modulated,Oversample);
% Initial_Sync_Preamble_Upsampled = upsample(Initial_Sync_Preamble,Oversample);

Initial_Sync_Preamble_Upsampled = Original_Signal...
    (HF_SSRRC_Len+1:HF_SSRRC_Len+(184)*Oversample);
Mini_Probe_Plus_Modulated_Upsampled = Original_Signal...
    (HF_SSRRC_Len+184*Oversample+1:HF_SSRRC_Len+184*Oversample+31*Oversample);


% detect the location of first symbol in data signal
%---------------------------------------------------
sync_flag = Paket_Start;
First184 = 0;
while (First184 == 0 )
    % take the first 184 symbols in incoming sensed signal when sync_flag
    % is zero value and shift the taken signal by sync_flag incrementing
    First184_RxInFiltered = RX_Faded_Noisy(sync_flag:sync_flag+2*184*Oversample-1);
    % calculate first 184 symbols energy
    First184_Energy = sum(abs(First184_RxInFiltered(1:1*184*Oversample)));
    % correlate between first 184 symbols in sensed signal and initial sync
    % preamble symbols (184 symbols)
    
    yy=conv( conj(fliplr(Initial_Sync_Preamble_Upsampled)) , (First184_RxInFiltered) );
    sync_flag =sync_flag+1*184*Oversample;
    
    %check from the max value of correlated signal if the sensed signal is
    %real signal or noise by comparing it with threshold
    
    Th_real = First184_Energy/1.5;
    
    %     max(abs(yy))
    if length(find(abs(yy) > Th_real)) > 1
        % if there are two or more places in correlated signal then it is
        % real data signal and not noise
        First184 = 1;
    end
end

% get the peak position in correlated signal (the last sample in the
% initial preamble // MH
[Preamble_places N_peaks] = GetPeak_Indecies(abs(yy),Th_real, 2);

if(N_peaks == 1)
    [Preamble_Place1 N_peaks] = GetPeak_Indecies...
    (abs(yy(Preamble_places-Oversample*5-(Oversample+1):Preamble_places-Oversample*5+(Oversample+1))),Th_real/8, 1);
    Preamble_Place1 = Preamble_Place1 + Preamble_places - Oversample*5 - (Oversample+1) - 1;
    Preamble_Place2 = Preamble_places;
    
    if(isempty(Preamble_Place1))
        InitialPreamble_Place = Preamble_Place2+Paket_Start-1;
    else
        InitialPreamble_Place = Preamble_Place1+Paket_Start-1;
    end
else
    InitialPreamble_Place = Preamble_places(1)+Paket_Start-1;
end


% idealy the phase of peak shall be zero, but due to channel error we may
% to correct it if there is phase error
Phase_Shift = angle(max(yy(Preamble_places)));


% detect the location of first symbol in mini-probe preamble
%-----------------------------------------------------------
First31 = 0;
First31Symbol_Preample = Initial_Sync_Preamble_Upsampled(1:31*Oversample);

while (First31 == 0 )
    % take the first 31 symbols after which discovered -take the first 31 from
    % undiscovered signal- from sensed signal
    First31_RxInFiltered = RX_Faded_Noisy(InitialPreamble_Place:InitialPreamble_Place+31*Oversample-1);
    % correlate it with the first 31 in initial preamble
    Withfirst31preamble = conv(First31_RxInFiltered , conj(fliplr(First31Symbol_Preample)));
    % correlate it with the mini-probe symbols
    Withminiprobe = conv(First31_RxInFiltered , conj(fliplr(Mini_Probe_Plus_Modulated_Upsampled)));
    
    if max(abs(Withminiprobe)) > max(abs(Withfirst31preamble))
        % the undiscovered 31 symbols is for miniprobe
        First31 = 1;
    else
        % pass the 184 initial preamble symbols
        InitialPreamble_Place = InitialPreamble_Place + Oversample*184;
    end
end

% First Sample of last synchronization preamble (the first of the last 184 symbols)
First_Sample = InitialPreamble_Place - Oversample*184;


% RX_Faded_Noisy = oversample(RX_Faded_Noisy,20);
% RX_Faded_Noisy =  conv(RX_Faded_Noisy,HF_SRRC);






% x1 = angle((RX_Faded_Noisy(First_Sample+1:First_Sample+184*4+31*4)));
% x2 = x1(2:end);
% x_r = x2-x1(1:end-1);
% o1 = angle((Original_Signal(48+1:48+184*4+31*4)));
% o2 = o1(2:end);
% o_r = o2-o1(1:end-1);
% f_r = mean(x_r-o_r);
% PFO_setimation = f_r


% fin = 10;
% lut = fliplr(0.001:(0.125-0.001)/fin:0.125-(0.125-0.001)/fin);
% hh = [];
% len = 184;
% for loop=1:fin
% Initial_Sync_Preamble_Upsampled_orig = Original_Signal(48+1:48+len*4);
% Rx_Work = RX_Faded_Noisy(First_Sample:First_Sample+len*4+1);
% Actual_Rx1 = interp1(1:length(Rx_Work),Rx_Work,(1-0.25):length(Rx_Work));
% Actual_Rx2 = interp1(1:length(Rx_Work),Rx_Work,(1+0.25):length(Rx_Work));
% Actual_Rx =  [Actual_Rx1(2:end-1);...
%               Actual_Rx2(2:end)];
% x=zeros(1,2);
% for range =1:2
%     x(range) = abs(sum(conj(Initial_Sync_Preamble_Upsampled_orig).*Actual_Rx(range,:)));
% end
% if x(1) > x(2)
%     hh = [hh -lut(loop)];
% else
%     hh = [hh  lut(loop)];
% end
% RX_Faded_Noisy = interp1(1:length(RX_Faded_Noisy),RX_Faded_Noisy,(1+hh(end)):length(RX_Faded_Noisy));
% RX_Faded_Noisy(isnan(RX_Faded_Noisy)) = 0;
% end
% 
% PFO_setimation = [PFO_setimation ; sum(hh)]
% 
% 
% x=1;




% Phase_Shift = angle(yy(InitialPreamble_Place));
end





